<template>
	<view class="content" style="padding-bottom: 150rpx;">
		<u-sticky bgColor="#f5f5f5">
			<view class="mainpadding flexbetween">
				<view class="xiaohong titletext bold" style="width: 100rpx;" @click="show=true">筛选</view>
				<view class="flexbetween ssbox" style="background-color: #fff;width: 100%;">
					<view class="flexleft">
						<u-icon name="search" color="#999" size="20"></u-icon>
						<input type="text" placeholder="请输入搜索岗位" class="hei_text main_size bold" v-model="keyword"
							confirm-type="search" @confirm="getWorkList()">
					</view>
					<view class="rightss" @click="getWorkList">搜索</view>
				</view>
			</view>
		</u-sticky>
		
		<view>
			<view class="items" v-for="item in workList" :key="item.id" @click="toXq(item.id)">
				<view class="lie">
					<view class="max-text">{{ item.title }}</view>
					<view class="max-text">{{ item.name }}</view>
				</view>
				<view class="lie">
					<view class="text-color" style="font-size: 28rpx; font-weight: 600">{{
            item.xname
          }}</view>
				</view>
				<view class="bet">
					<view class="flex">
						<view v-for="(items, idx) in item.fulilabels" :key="items.id">
							<y-tag v-if="idx < 3" :text="items.name" bgcolors="#efefef" color="#000000" />
						</view>
						<view v-if=" item.fulilabels !== undefined && item.fulilabels.length > 3">...</view>
					</view>
					<view class="min-text">{{ item.create_time }}</view>
				</view>
			</view>
			<u-empty v-if="workList.length == 0" mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" />
		</view>
		<view class="add" @click="changeTab()">
			<view>{{ from.id ? "修改" : "发布" }}</view>
		</view>
		<tabbar :current="2" ref="tabbarRef"></tabbar>
		<u-popup :show="show" @close="show=false" mode="bottom" :round="10">
			<view class="mainpadding">
				<view class="titletext textcenter">筛选</view>
				<view class="margin_top strongtext">时间</view>
				<view class="flexleft flex_wrap">
					<view class="flexcenter sanshi margin_top2" v-for="item in timeList" @click="selecTimeBtn(item.id)">
						<view :class="item.id==selecttime?'haungsebtn':'huisebtn'">{{item.name}}</view>
					</view>
				</view>
				<!-- 时间 -->
				<view class="margin_top strongtext">自定义时间</view>
				<view class="flexcenter margin_top2" @click="zdysj">
					<view class="shijianbtn margin_right3" @click="zshijian">{{startime}}</view>
					<view class="xian margin_right3"></view>
					<view class="shijianbtn" @click="yshijian">{{endtime}}</view>
				</view>
				<view class="flexbetween margin_top2 ssbox" style="background-color: #f7f9fa; padding: 20rpx;" @click="shows = true">
					<input type="text" disabled="" placeholder="点击选择城市" style="width: 100%;" class="hei_text main_size bold"
						v-model="address">
					<u-icon name="arrow-right" color="#999" size="20"></u-icon>
				</view>
				<u-datetime-picker :show="zshow" @confirm="zconfirm" v-model="value1" mode="date"
					@cancel="zshow= false"></u-datetime-picker>
				<u-datetime-picker :show="yshow" @confirm="yconfirm" v-model="value2" mode="date" @cancel="yshow= false"
					:minDate="minDate"></u-datetime-picker>

				<view class="flexbetween">
					<view class="chonzbtn margin_top" @click="chongzhi">重置</view>
					<view class="quedbtn1 margin_top" @click="close">确认</view>
				</view>
			</view>
		</u-popup>
		<u-picker :show="shows" ref="uPicker" :columns="columns1" @confirm="confirm" @cancel="shows = false"
			@change="changeHandler"></u-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import yTag from "@/components/y-tag/index";
	export default {
		name: "tab-1",
		components: {
			yTag,
		},
		data() {
			return {
				from: {},
				workList: [],
				userIdentity: "普通用户",
				keyword: "",
				// 时间
				startime: "开始时间",
				endtime: "结束时间",
				// 数据列表
				show: false,
				minDate: "",
				//开始时间
				zshow: false,
				value1: Number(new Date()),
				//结束
				yshow: false,
				value2: Number(new Date()),
				// 筛选
				timeList: [{
						id: 1,
						name: "今日"
					},
					{
						id: 2,
						name: "昨日"
					},
					{
						id: 3,
						name: "本月"
					},
					{
						id: 4,
						name: "上月"
					}
				],
				selecttime: 0, //筛选时间类型
				provinces: "",
				citys: "",
				qus: "",
				areaList: [],
				address: "",
				columns1: [],
				shows: false,
			};
		},
		async onLoad() {
			uni.hideTabBar()
			// if (!uni.getStorageSync("token")) {
			// 	// #ifdef APP-PLUS
			// 	uni.navigateTo({
			// 		url: "/pages/login/applogin",
			// 	});
			// 	// #endif
			// 	// #ifdef MP-WEIXIN
			// 	uni.navigateTo({
			// 		url: "/pages/login/login",
			// 	});
			// 	// #endif
			// 	return;
			// }
			this.getAreaList();
		},
		onShow() {
			this.$nextTick(function() {
				this.$refs.tabbarRef.$emit("childmethod")
			});
			this.getWorkOne();
			this.getWorkList();

			this.userIdentity = this.judgeIdentity();
		},
		methods: {
			async getAreaList() {
				const province = [];
				const city = [];
				const county = [];
				const a = await this.http.get("index/area"); // a.data
				a.data.forEach((item,index)=>{
					item.children.unshift({
						name:"不限",
						id:"f"+index,
						children:[{
								name:"不限",
								id:"f"+index+index,
							}]
					})
					item.children.forEach((ite,ind)=>{
						if(ite.children){
							ite.children.unshift({
								name:"不限",
								id:"f"+index+ind,
							})
						}
						
					})
					
				})
				this.areaList = a.data;
				console.log(this.areaList, "columns1");
				a.data.map((item) => {
					province.push(item.name);
				});
				a.data[0].children.map((item) => {
					city.push(item.name);
				});
				a.data[0].children[0].children.map((item) => {
					county.push(item.name);
				});
				this.columns1 = [province, city, county];
				console.log(this.columns1, "columns1");
			},
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					indexs,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker,
				} = e;
				const city = [];
				const county = [];
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				if (columnIndex === 0) {
					this.areaList[indexs[0]].children.map((item) => {
						city.push(item.name);
					});
					this.areaList[indexs[0]].children[indexs[1]].children.map((item) => {
						county.push(item.name);
					});
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(1, city);
					picker.setColumnValues(2, county);
				} else if (columnIndex === 1) {
					this.areaList[indexs[0]].children[indexs[1]].children.map((item) => {
						county.push(item.name);
					});
					picker.setColumnValues(2, county);
				}
			},
			// 回调参数为包含columnIndex、value、values
			confirm(e) {
				console.log(e, "98888");
				this.provinces = this.areaList[e.indexs[0]].id
				this.citys = e.value[1]=="不限"?"":this.areaList[e.indexs[0]].children[e.indexs[1]].id
				this.qus =  e.value[2]=="不限"?"":this.areaList[e.indexs[0]].children[e.indexs[1]].children[e.indexs[2]].id
				this.address = e.value.join();
				this.shows = false;
			},
			judgeIdentity() {
				//获取用户信息判断用户账号权限身份

				let userInfo = JSON.parse(uni.getStorageSync('userInfo')).data;
				if (!userInfo) {
					return undefined;
				}
				// console.log("找工作模块获取用户信息");
				// console.log(userInfo.group_id);
				// console.log(userInfo);

				if (userInfo.group_id == 1) {
					//1普通用户
					return "普通用户";
				}

				if (userInfo.group_id == 2) {
					//2驻场
					return "驻场";
				}

				if (userInfo.group_id == 3) {
					//3文员
					return "文员"
				}
			},
			async getWorkOne() {
				if (!uni.getStorageSync("token")) return;
				const data = await this.http.get("work/one");
				this.from = {
					id: data.data.id
				};
			},
			changeTab() {
				if (!uni.getStorageSync("token")) {
					// #ifdef APP-PLUS
					uni.navigateTo({
						url: "/pages/login/applogin",
					});
					// #endif
					// #ifdef MP-WEIXIN
					uni.navigateTo({
						url: "/pages/login/login",
					});
					// #endif
					return;
				}
				uni.navigateTo({
					url: "/pages/tab3/fabu",
				});
			},
			getWorkList() {
				if (!uni.getStorageSync("token")) return;
				this.http
					.get("work/index", {
						keyword: this.keyword,
						start_birthday: this.startime == "开始时间" ? '' : this.startime,
						end_birthday: this.endtime == "结束时间" ? '' : this.endtime,
						province:this.provinces,
						city:this.citys,
						qu:this.qus,
					})
					.then((res) => {
						this.workList = res.data;
					})
					.catch((err) => {
						console.warn(err, "err");
					});
			},
			toXq(id) {
				uni.navigateTo({
					url: "/pages/tab2/details?type=qiuzhi&id=" + id,
				});
			},
			chongzhi() {
				this.selecttime = ""
				this.startime = "开始时间"
				this.endtime = "结束时间"
				this.address = ""
				this.provinces = ""
				this.citys = ""
				this.qus = ""
				this.show = false
				this.getWorkList()
			},
			// 选择时间段
			selecTimeBtn(id) {
				this.mindate = ""
				this.selecttime = id
				if (id == 1) {
					// 获取今日
					this.startime = this.riqi(0);
					this.endtime = this.riqi(0);
				} else if (id == 2) {
					// 获取昨日
					this.startime = this.riqi(-1);
					this.endtime = this.riqi(-1);
				} else if (id == 3) {
					// 获取本月
					this.startime = this.benyue();
					this.endtime = this.riqi(0);
				} else {
					this.startime = this.shangyue(1);
					this.endtime = this.shangyue(2);
				}
			},
			// 昨天“-1”今天“0”明天“1”
			riqi(day) {
				let dd = new Date();
				dd.setDate(dd.getDate() + day);
				let y = dd.getFullYear();
				let m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
				let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
				return y + "-" + m + "-" + d;
			},
			// 上月 1第一天，2最后一天
			shangyue(val) {
				var nowdays = new Date()
				var year = nowdays.getFullYear()
				var month = nowdays.getMonth()
				if (month == 0) {
					month = 12
					year = year - 1
				}
				if (month < 10) {
					month = '0' + month
				}
				var myDate = new Date(year, month, 0)
				var startDate = year + '-' + month + '-01' //上个月第一天
				var endDate = year + '-' + month + '-' + myDate.getDate() //上个月最后一天
				if (val == 1) {
					return startDate
				} else {
					return endDate
				}
			},
			// 本月第一天
			benyue() {
				let y = new Date().getFullYear(); //获取年份
				let m = new Date().getMonth() + 1; //获取月份
				let d = '01'
				m = m < 10 ? '0' + m : m; //月份补 0
				return [y, m, d].join("-")
			},
			date(timer) {
				var date = new Date(timer); //当前时间
				var year = date.getFullYear() //返回指定日期的年份
				var month = repair(date.getMonth() + 1); //月
				var day = repair(date.getDate()); //日
				var curTime = year + "-" + month + "-" + day

				function repair(i) {
					if (i >= 0 && i <= 9) {
						return "0" + i;
					} else {
						return i;
					}
				}
				return curTime
			},
			// 开始时间
			zconfirm(e) {
				console.log(e)
				this.selecttime = ""
				this.minDate = e.value
				this.startime = this.date(e.value)
				this.zshow = false
			},
			zshijian() {
				this.zshow = true
			},
			//结束时间
			yconfirm(e) {
				console.log(e)
				this.selecttime = ""
				this.endtime = this.date(e.value)
				this.yshow = false
			},
			yshijian() {
				if (this.minDate == "") {
					httpRequest.toast("请选择开始时间")
					return
				}
				this.yshow = true
			},
			close() {
				this.getWorkList()
				this.show = false
			}
		},
	};
</script>
<style lang="scss" scoped>
	.items {
		padding: 20rpx;
		background: #ffffff;
		opacity: 1;
		margin: 0rpx 30rpx 30rpx;
		border-radius: 10rpx;
		align-items: center;

		.lie {
			display: flex;
			justify-content: space-between;
			margin-bottom: 20rpx;

			.sex {
				font-size: 24rpx;
			}
		}
	}

	.add {
		width: 80rpx;
		position: fixed;
		bottom: 200rpx;
		height: 80rpx;
		font-size: 28rpx;
		right: 50rpx;
		padding: 10rpx;
		z-index: 99;
		background-color: #c1282d;
		border-radius: 100rpx;
		color: white;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.ssbox {
		position: relative;
		padding: 5rpx 5rpx 5rpx 30rpx;
		border-radius: 50rpx;

		.rightss {
			min-width: 150rpx;
			text-align: center;
			box-sizing: border-box;
			padding: 20rpx 40rpx;
			border-radius: 50rpx;
			color: #fff;
			font-size: 24rpx;
			background-color: #c1282d;
		}
	}
</style>
<style lang="scss" scoped>
	.quedbtn1 {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 321rpx;
		height: 82rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #fff;
		border: 2rpx solid #c1282d;
		background-color: #c1282d;
		border-radius: 41rpx;
	}

	.chonzbtn {
		border-radius: 41rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 321rpx;
		height: 82rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #c1282d;
		border: 2rpx solid #c1282d;
		background-color: #fff;
	}

	.chanpinse {
		font-size: 32rpx;
		font-family: DIN Next LT Pro-Bold, DIN Next LT Pro;
		font-weight: bold;
		color: #c1282d;
	}

	.kym {
		margin: 250rpx auto 0;
		width: 343rpx;
		height: 324rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.xian {
		width: 22rpx;
		height: 0rpx;
		background-color: #999999;
		border: 1rpx solid #999999;
	}

	.shijianbtn {
		padding: 20rpx 65rpx;
		background: #F7F9FA;
		border-radius: 41rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.huisebtn {
		width: 198rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
		background: #F7F9FA;
		border-radius: 41rpx;
	}

	.haungsebtn {
		width: 198rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #c1282d;
		background: rgba(193, 40, 45, .1);
		border-radius: 41rpx;
	}

	.sanshi {
		width: 33.33%;
	}
</style>